//
//  GridsTextViewCellTests.swift
//  AmountBodyTests
//
//  Created by Qiu Jishuai on 2018/1/30.
//  Copyright © 2018年 RCMTM. All rights reserved.
//

import XCTest
@testable import AmountBody

class GridsTextViewCellTests: XCTestCase {

    let cell = StubCell()

    override func setUp() {
        super.setUp()
        // Put setup code here. This method is called before the invocation of each test method in the class.
    }
    
    override func tearDown() {
        // Put teardown code here. This method is called after the invocation of each test method in the class.
        super.tearDown()
    }


    func testSubLayersCountShouldEqualsConfigs() {

        let configs: [TextGridView.Cell.TextConfig] = [("test", 0.6), ("text", 0.4)]
        cell.configs = configs
        cell.layoutSublayers()
        XCTAssertEqual(configs.count, cell.sublayers?.count ?? 0)

        cell.configs = []
        cell.layoutSublayers()
        XCTAssertEqual(0, cell.sublayers?.count ?? 0)
    }

    func testConfigsCountNotEqualLayersCountShouldReturn() {
        let configs: [TextGridView.Cell.TextConfig] = [("test", 0.6), ("text", 0.4)]
        cell.configs = configs
        cell.textLayers.removeLast()
        cell.layoutSublayers()
       XCTAssertFalse(cell.isLayouted)
    }

    func testSublayerScalesShouldEqualsConfigs() {
        let configs: [TextGridView.Cell.TextConfig] = [("test", 0.6), ("text", 0.4)]
        cell.frame = CGRect(x: 0, y: 0, width: 100, height: 100)
        cell.configs = configs
        cell.layoutSublayers()

        for (index, config) in configs.enumerated() {
            let width = cell.textLayers[index].frame.width
            XCTAssertEqual(width, round(config.scale * cell.frame.width))
        }
    }

    class StubCell: TextGridView.Cell {

        var isLayouted = false

        override func layoutSublayers() {
            super.layoutSublayers()
        }
    }
}
